﻿using System;
using System.Collections;

namespace Eneta.Portal.Forum.Rss
{
    public class RssItemCollection : CollectionBase
    {
        // Fields
        private DateTime latestPubDate = RssDefault.DateTime;
        private DateTime oldestPubDate = RssDefault.DateTime;
        private bool pubDateChanged = true;

        // Methods
        public int Add(RssItem item)
        {
            this.pubDateChanged = true;
            return base.List.Add(item);
        }

        private void CalculatePubDates()
        {
            if (this.pubDateChanged)
            {
                this.pubDateChanged = false;
                this.latestPubDate = DateTime.MinValue;
                this.oldestPubDate = DateTime.MaxValue;
                foreach (RssItem item in base.List)
                {
                    if ((item.PubDate != RssDefault.DateTime) & (item.PubDate > this.latestPubDate))
                    {
                        this.latestPubDate = item.PubDate;
                    }
                }
                if (this.latestPubDate == DateTime.MinValue)
                {
                    this.latestPubDate = RssDefault.DateTime;
                }
                foreach (RssItem item2 in base.List)
                {
                    if ((item2.PubDate != RssDefault.DateTime) & (item2.PubDate < this.oldestPubDate))
                    {
                        this.oldestPubDate = item2.PubDate;
                    }
                }
                if (this.oldestPubDate == DateTime.MaxValue)
                {
                    this.oldestPubDate = RssDefault.DateTime;
                }
            }
        }

        public bool Contains(RssItem rssItem)
        {
            return base.List.Contains(rssItem);
        }

        public void CopyTo(RssItem[] array, int index)
        {
            base.List.CopyTo(array, index);
        }

        public int IndexOf(RssItem rssItem)
        {
            return base.List.IndexOf(rssItem);
        }

        public void Insert(int index, RssItem item)
        {
            this.pubDateChanged = true;
            base.List.Insert(index, item);
        }

        public DateTime LatestPubDate()
        {
            this.CalculatePubDates();
            return this.latestPubDate;
        }

        public DateTime OldestPubDate()
        {
            this.CalculatePubDates();
            return this.oldestPubDate;
        }

        public void Remove(RssItem item)
        {
            this.pubDateChanged = true;
            base.List.Remove(item);
        }

        // Properties
        public RssItem this[int index]
        {
            get
            {
                return (RssItem)base.List[index];
            }
            set
            {
                this.pubDateChanged = true;
                base.List[index] = value;
            }
        }
    }
}
